package com.arithmetic.version2;

import java.util.Random;

public abstract class BinaryOperation {
    protected int leftOperand;
    protected int rightOperand;
    protected char operator;
    protected int value;

    public BinaryOperation() {}

    public BinaryOperation(int left, int right, char op) {
        this.leftOperand = left;
        this.rightOperand = right;
        this.operator = op;
        this.value = calculate();
    }

    // 访问器方法
    public int getLeftOperand() { return leftOperand; }
    public int getRightOperand() { return rightOperand; }
    public char getOperator() { return operator; }
    public int getValue() { return value; }

    // 抽象方法
    public abstract int calculate();
    public abstract boolean checkingCalculation(int result);

    @Override
    public String toString() {
        return leftOperand + " " + operator + " " + rightOperand + " =  \t";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        BinaryOperation that = (BinaryOperation) obj;
        return leftOperand == that.leftOperand &&
                rightOperand == that.rightOperand &&
                operator == that.operator;
    }

    public static BinaryOperation generateOperation(char operator) {
        Random rand = new Random();
        if (operator == '+') {
            return new AdditionOperation(rand.nextInt(100), rand.nextInt(100));
        } else if (operator == '-') {
            int left = rand.nextInt(100);
            int right = rand.nextInt(left + 1);
            return new SubstractOperation(left, right);
        }
        return null;
    }
}
